package com.fling.abcde.wxapi;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

import com.lib.utils.AppUtils;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;

import org.json.JSONObject;

import static com.fling.abcde.BaseApplication.sContext;

/**
 * @author FengLing
 * Created on 2017-06-28 15:46
 */
public class QQApiUtils {
	
	public static Tencent sQqApi;
	
	public static void qqLogin(Activity activity, BaseUiListener mQqLoginListener) {
		
		if (judgeCanGo()) {
			return;
		}
		sQqApi.login(activity, "all", mQqLoginListener);
	}
	
	public static void shareToQQ(Activity activity, Bundle bundle, IUiListener listener) {
		
		if (judgeCanGo()) {
			return;
		}
		sQqApi.shareToQQ(activity, bundle, listener);
	}
	
	public static void shareToQZone(Activity activity, Bundle bundle, IUiListener listener) {
		
		if (judgeCanGo()) {
			return;
		}
		sQqApi.shareToQzone(activity, bundle, listener);
	}
	
	private static boolean judgeCanGo() {
		
		if (!AppUtils.isAppInstalled(sContext, "com.tencent.mobileqq")) {
			Toast.makeText(sContext, "请先安装QQ...", Toast.LENGTH_SHORT).show();
			return true;
		}
		if (sQqApi == null) {
			sQqApi = Tencent.createInstance("101415484", sContext);
		}
		return false;
	}
	
	public static class BaseUiListener implements IUiListener {
		
		@Override
		final public void onComplete(Object response) {
			
			if (null == response) {
				return;
			}
			JSONObject jsonResponse = (JSONObject) response;
			if (jsonResponse.length() == 0) {
				return;
			}
			doComplete((JSONObject) response);
		}
		
		protected void doComplete(JSONObject values) {
		
		}
		
		@Override
		public void onError(UiError e) {
		
		}
		
		@Override
		public void onCancel() {
		
		}
	}
}